﻿@using FController = Host.Controllers.FundController
@using Host.Models
@model Host.Models.RuleModel
@{
    ViewBag.Title = "Fund Rules";
}

<h2>
    Fund Rules</h2>
    
        <p style="width: 70%;" >
            This page is designed to allow the customization of the rules that control the validation
            of the items in the import data.
        </p>
   <table >
            <tr>
                <td style=" text-align:center;">
               
                    <button onclick="ShowCreateDialog()" class="button tip" id="CreateButton" title="This button,  will show a new dialog to allow the creating of a rule
                                                                                    in the system * NOTE: the checkbox 'Is General Rule' is a flag for the rule to be associated
                                                                                            with all funds and not just for the currently selected one">
                        Create Rule</button>     
                </td>
                <td style="text-align: left;">
                    <button onclick="ShowMapDialog()" class="button tip" title="This button, will show a dialog to allow you to associate a rule to a fund with the respective answer. ">
                        Associate Question to Fund</button>
                </td>
            </tr>
        </table>
@using (Html.BeginForm("Rules", "Fund", FormMethod.Post, new { id = "f" }))

{
    <fieldset>
        <legend>Current Rules</legend>
        <table>
            <tr>
                <td>
                    Fund:
                </td>
                <td >
                    @Html.DropDownListFor(model => model.FundId, new SelectList(Model.Funds, "Value", "Text"), new { @id = "ddlFund" })
                  
                </td>
            </tr>
            <tr>
                <td style="width: 120px;">
                    Question Group:
                </td>
                <td>
                    @Html.DropDownListFor(model => model.QuestionGroup, new SelectList(Model.QuestionGroups, "Value", "Text"), new { @id = "ddlQuestionGroup" })
            
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div >
                    @{ 
                        
    var grid = new WebGrid(canPage: true, rowsPerPage: FController.QuestionPageSize, canSort: true, ajaxUpdateContainerId: "grid", ajaxUpdateCallback: "updateStyle");
    grid.Bind(Model.Questions, rowCount: Model.TotalRecords, autoSortAndPage: false);
    grid.Pager(WebGridPagerModes.All);
                                                        @grid.GetHtml(
                                                   tableStyle: "ui-grid-content ui-widget-content",
                                                   headerStyle: "ui-grid-header ui-widget-header ui-corner-top ui-state-default",
                                                   footerStyle: "ui-grid-footer ui-widget-header ui-corner-bottom ",
                                                   alternatingRowStyle: "webgrid-alternating-rows",
                                    htmlAttributes: new { id = "grid" },
                                    columns: grid.Columns(
                                        grid.Column("Description"),
                                        grid.Column("Answer"),
                                        grid.Column("AnswerType"),
                                        grid.Column(format: @<a href='#' onclick="Edit(@item.FundAnswerSheetId)" >Edit</a>)
                                    ));
                        }
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>
}
   

<div id="CreateDialog" title="Rule administration">
    <form id="createForm" action="">
     <fieldset>
        <legend>Create the rule for the fund</legend>
        <table style="width: 100%;">
            <tr>
                <td colspan="2">
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    Question:
                </td>
                <td>
                    <input type="text" id="questionDescription" name="questionDescription" class="tip" style="width: 90%;" title="This is the question that will be shown in the reports." />
                </td>
            </tr>
            <tr>
                <td style="width: 190px;">
                    Expected Answer Type:
                </td>
                <td>
                    
                    <select id="Answertype"  >
                    @foreach (var type in new Host.Models.Data().AnswerDataType)
                        {
                            <option value="@type.AnswerDataTypeId">
                                @switch (type.AnswerDataTypeDescription)
                                {
                                    case "Boolean":
                                        Write("True / False");
                                        break;
                                    case "String":
                                        Write("Text");
                                        break;
                                    case "Integer":
                                        Write("Number");
                                        break;
                                    case "Numeric":
                                        Write("Decimal Number");
                                        break;
                                    case "Count":
                                        Write("Count");
                                        break;
                                    case "Compare":
                                        Write("Compare");
                                        break;
                                }
                            </option>
                        }
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    Question Category:
                </td>
                <td>
                    @Html.DropDownListFor(model => model.FundId, new SelectList(Model.QuestionCategorys, "Value", "Text"), new { @id = "ddlQuestionCategorys" })
                </td>
            </tr>
            <tr>
                <td>Is General Rule:</td>
                <td>
                    <div class="checkbox">
                        <input type="checkbox" name="GenRule" id="GenRule" />
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    Question Group:
                </td>
                <td>
                    <select id="QuestionGroupForm" name="QuestionGroupForm" style="width: 420px;">
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <br />
                    <div class="info" style="width:100%;">
                    <table >
                        <tr style="vertical-align: top;">
                            <td>
                                <div class="remember">
                                </div>
                            </td>
                            <td style="padding-left: 10px; font-size: 11px;">
                                To create a rule for the selected fund, type the question words in the provided
                                text area, select the appropriate expected answer type and the question category
                                that the question belongs with the question group that will be used to identify
                                the question. Click the save button if all is satisfactory.
                                <br />
                                <ul>
                                    <li>I.E. If there are no question group items when selecting the question category then
                                        use the "Create group relationship" button to create a relationship between the
                                        question category and the question group. </li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>
     <fieldset>
        <legend>Create the relationship for Question Group </legend>
        <table>
            <tr>
                <td colspan="2">
                    <br />
                    <button onclick="showCatelogMap()">
                        Create group relationship</button>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <br />
                    <div class="info" style="width:100%;">
                    <table>
                        <tr style="vertical-align: top;">
                            <td>
                                <div class="remember">
                                </div>
                            </td>
                            <td style="padding-left: 10px; font-size: 11px;">
                                The button "Create group relationship" will show a new dialog to allow the creating
                                of the relationship between a question category and a question group.
                                <br />
                                <ul>
                                    <li>I.E. This is only to be used if there are no items in the selection option when
                                        changing the Question Category. </li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>
    </form>
</div>
<div id="CreateMapForCategory" title="Create question group to category relationship">
    <table style="width: 100%;">
        <tr>
            <td style="width: 200px;">
                Question Group:
            </td>
            <td>
                <input type="text" name="mapQuestionGroup" id="mapQuestionGroup" style="width: 95%;" />
            </td>
        </tr>
        <tr>
            <td>
                Question Category:
            </td>
            <td>
                <select name="mapQuestioCategory" id="mapQuestioCategory" style="width: 160px;">
                    @foreach (var QuestionCategory in new Host.Models.Data().QuestionCategory)
                    {
                        <option value="@QuestionCategory.QuestionCategoryId">@QuestionCategory.Category</option>
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            <div class="info" style="width:100%;">
                <table>
                    <tr style="vertical-align: top;">
                        <td>
                            <div class="remember">
                            </div>
                        </td>
                        <td style="padding-left: 10px; font-size: 11px;">
                            Select the category that you would like to associate the new question group.
                            <br />
                            <ul>
                                <li>click save to to save the new question group to the system. </li>
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>
            </td>
        </tr>
    </table>
</div>
<div id="fundRuleMapping" title="Associate a question to a fund">
    <table style="width: 100%;">
        <tr>
            <td style="width: 200px;">
                Fund:
            </td>
            <td>
                <select id="fundOption" name="fundOption" style="width: 160px;">
                    @foreach (var funditem in new Host.Models.Data().Fund)
                    {
                        <option value="@funditem.FundId">@funditem.FundName</option>
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Question Category:
            </td>
            <td>
                <select name="questionCategoryption" id="questionCategoryption" style="width: 160px;">
                    @foreach (var questionCategoryItem in new Host.Models.Data().QuestionCategory)
                    {
                        <option value="@questionCategoryItem.QuestionCategoryId">@questionCategoryItem.Category</option>
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Question Group:
            </td>
            <td>
                <select id="questionGroupOption" name="questionGroupOption" style="width: 420px;">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Question:
            </td>
            <td>
                <select name="questionOption" id="questionOption" style="width: 500px;">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Answer:
            </td>
            <td>
                <input type="text" id="answerOption" name="answerOption" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div class="info" style="width: 100%;">
                    <table>
                        <tr style="vertical-align: top;">
                            <td>
                                <div class="remember">
                                </div>
                            </td>
                            <td style="padding-left: 10px; font-size: 11px;">
                                To create a relationship for the question to the fund use the options provided.
                                <br />
                                <ul>
                                    <li>Select the fund that requires the question to be validated</li>
                                    <li>Select the category that question group belongs to</li>
                                    <li>Select the question</li>
                                    <li>Insert the expected answer</li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
</div>

<div title="Edit" class="dialog">
    <table style="width: 100%;">
        <tr>
            <td>
                <div class="info">
                    <table id="TopDescription" style="padding-left: 30px;">
                        <tr style="vertical-align: top;">
                            <td style="padding: 10px 10px 10px 10px;">
                                <div class="remember">
                                </div>
                            </td>
                            <td style="padding-left: 10px; font-size: 11px;">
                                The button "Create rule" will show a new dialog to allow the creating of a rule
                                in the system, the checkbox "Is General Rule" is a flag for the rule to be associated
                                with all funds and not just for the currently selected one.
                                <br />
                                <ul>
                                    <li>This is to be used when rules are need for the fund and question group. </li>
                                    <li>To view different rules for the fund, change the fund selection option and the question
                                        group option.</li>
                                    <li>To create a question association click the "Associate question to fund" button".</li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
</div>

<script id="GlobalScripts" type="text/javascript">
    var CreateForm;
    var Fund;
    var FundQuestionGroup;
    var Current_Questions;
    var Dialog_QuestionGroup_Category;
    var Dialog_Associate_Question_To_Fund;
    var Dialog_Create_Rule;
    var Fund_Rule_Map_Category;
    var Fund_Rule_Fund_Option;
    var Fund_Rule_QuestionGroup_Option;
    var Fund_Rule_Question_Option;
    var Fund_Rule_Answer_Option;
    var Map_Question_Group;
    var Map_Question_Category;
    var Create_Question_Description;
    var Create_Answer_DataType;
    var Create_Question_Category;
    var Create_Gen_Rule;
    var Create_Question_Group_Form;
</script>

<script id="CreateDialogScripts" type="text/javascript">
    function Create_Make_Rule_Dialog()
    {
        $( "#CreateDialog" ).dialog( {//Create the dialog control 
            autoOpen: false, //set the auto open [so that the page load does not show the dialog
            resizable: false, //fix the size of the control
            height: 755,
            width: 950,
            modal: true, //set the model property to block user intervention on the page
            buttons: {
                'Save': function ()
                { //Save method that will be called when dialog save button is clicked
                    $( "#createForm" ).validate();
                    if ( $( "#createForm" ).valid() )
                    {
                        saveCreateRule();
                        $( this ).dialog( 'close' ); //Close the dialog 
                    }
                },
                Cancel: function ()
                { //Close the dialog and reset controls when the cancel button is clicked
                    $( this ).dialog( 'close' ); //Close the dialog
                }
            }
        } );
        
    }
    function ShowCreateDialog()
    {
        $( "#CreateDialog" ).dialog( 'open' );
       // $( "select" ).selectmenu( {} );
       // $( ".tip" ).tooltip( { position: "center right", offset: [-2, 10], effect: "fade", opacity: 0.7 } );
        // $("#AnswerDataType").selectmenu({});
        //$("#questionCategory").selectmenu({});
        // GetQuestionGroupForCategory();

       // $( '#ddlAnswerTypes' ).select( function ( e ) { alert( 'it worked!' ); } );
        $( '#Answertype' ).change( function () { GetQuestions(); } );
    }
</script>

<script id="ChangeScripts" type="text/javascript">
    $( '#ddlFund' ).change( function (){ $( 'form#f' ).submit(); return false; } );
    $( '#ddlQuestionGroup' ).change( function () { $( 'form#f' ).submit(); return false; } );

   
</script>

<script id="QuestionScripts" type="text/javascript">
    function GetQuestions(){alert( "T" ); }
</script>
<script type="text/javascript">
    
    function Fileter()
    {
        $( 'form' ).submit(); 
     }

     $( function () { updateStyle(); } );
     function updateStyle()
     {
         $( ".ui-grid-footer" ).find( "a" ).each( function ( i, el ) { $( this ).button(); } );
         $( ".ui-grid-content tbody" ).find( "a" ).each( function ( i, el ) { $( this ).button(); } );
     }
     function Edit( id )
     {
         $( ".dialog" ).dialog( 'open' );
     }
     $( function ()
     {
      $('form#ruleForm').trigger('submit'); 

         $( ".dialog" ).dialog( {
             autoOpen: false, //set the auto open [so that the page load does not show the dialog
             resizable: false, //fix the size of the control
             height: 695,
             width: 950,
             modal: true, //set the model property to block user intervention on the page
             buttons: {
                 'Save': function ()
                 { //Save method that will be called when dialog save button is clicked
                     $( "#createForm" ).validate();
                     if ( $( "#createForm" ).valid() )
                     {
                         saveCreateRule();
                         $( this ).dialog( 'close' ); //Close the dialog 
                     }
                 },
                 Cancel: function ()
                 { //Close the dialog and reset controls when the cancel button is clicked
                     $( this ).dialog( 'close' ); //Close the dialog
                 }
             }
         } );
     } );
</script>
<script type="text/javascript">


/* Global */



var currentArray;

$( document ).ready( function ()
{//Page ready event [when the DOM is ready for processing


    $( "#ddlFund" ).selectmenu( {} );
    $( "#ddlQuestionGroup" ).selectmenu( {} );
    $( '._select_change' ).change( function () { alert( "p" ); } );
    $( '#ddlQuestionCategorys' ).change( function () { GetQuestions(); } );

    /* Set of global's*/
    SetGlobals();
    /*Validation*/

    /* Dialogs*/
    //-------------------------Create question group to category relationship -------------------------------//
    DialogQuestionGroupCategory();

    //-------------------------Associate a question to a fund -------------------------------//
    DialogAssociateQuestionToFund();

    Create_Make_Rule_Dialog();


    //GetMappedQuestions(); //Page load to get the mapping


} );



    //* ----------   Set the global variables that can be reused   --------------------*//
   function SetGlobals(){
       /*  -- Current Rules Form --*/
       Fund = $("#fund");
       FundQuestionGroup = $("#questionGroup");
       Current_Questions = $("#currentValues");
       //*-----CreateDialog  ------------id="CreateDialog" title="Rule administration" -----*//
       Dialog_Create_Rule = $("#CreateDialog");
       CreateForm = $("#createForm");
       Create_Question_Description = $("#questionDescription");
       Create_Answer_DataType = $("#AnswerDataType");
       Create_Question_Category = $("#questionCategory");
       Create_Gen_Rule = $("#GenRule");
       Create_Question_Group_Form = $("#QuestionGroupForm");

       /* - Fun Mapping Dialog---------------- id="fundRuleMapping" title="Associate a question to a fund"- */
       Dialog_QuestionGroup_Category = $("#CreateMapForCategory");
       Fund_Rule_Map_Category = $("#questionCategoryption");
       Fund_Rule_Fund_Option = $("#fundOption");
       Fund_Rule_QuestionGroup_Option = $("#questionGroupOption");
       Fund_Rule_Question_Option = $("#questionOption");
       Fund_Rule_Answer_Option = $("#answerOption");

       /*  Map for Category ------  id="CreateMapForCategory" title="Create question group to category relationship" */
       Dialog_Associate_Question_To_Fund = $("#fundRuleMapping");
       Map_Question_Group = $("#mapQuestionGroup");
       Map_Question_Category = $("#mapQuestioCategory");
   }


   /*  --------------Set validation -------------------------------------------------------------*/
   function Validation(){CreateForm.validate({ rules: { questionDescription: "required" }, messages: { questionDescription: "required"} });}

   /* ----------------*/
   function DialogQuestionGroupCategory(){
   Dialog_QuestionGroup_Category.dialog({
            autoOpen: false, //set the auto open [so that the page load does not show the dialog
            resizable: false, //fix the size of the control
            height: 380,
            width: 950,
            modal: true, //set the model property to block user intervention on the page
            buttons: {
                'Save': function () { //Save method that will be called when dialog save button is clicked
//                    var Model = { QuestionGroup: ko.observableArray([]), add: function () { } };
//                    Model.QuestionGroup.push({ QuestionCategoryId: $("#mapQuestioCategory").val(), Description: $("#mapQuestionGroup").val() });
//                    var d = ko.toJSON(Model.QuestionGroup);
//                    var a = $.parseJSON(d);

//                    $.ajax({
//                        url: '/Fund/CreateMapForCategory', type: "POST",
//                        data: JSON.stringify(a),
//                        contentType: 'application/json; charset=utf-8',
//                        success: function (data) { //Close if successful
//                            $(this).dialog('close'); //Close the dialog
//                        }
//                    });
                    $(this).dialog('close'); //Close the dialog
                    //update the group dropdown
                    GetQuestionGroupForCategory();

                },
                Cancel: function () { //Close the dialog and reset controls when the cancel button is clicked
                    $(this).dialog('close'); //Close the dialog
                }
            }
        });
   }

   /* ----------------*/
   function DialogAssociateQuestionToFund(){
   Dialog_Associate_Question_To_Fund.dialog({ 
            autoOpen: false, //set the auto open [so that the page load does not show the dialog
            resizable: false, //fix the size of the control
            height: 430,
            width: 950,
            modal: true, //set the model property to block user intervention on the page
            buttons: {
                'Save': function () { //Save method that will be called when dialog save button is clicked
                    //Save map
                    //QuestionId, FundId, AnswerValue
//                    var Model = { FundAnswerSheet: ko.observableArray([]), add: function () { } };
//                    Model.FundAnswerSheet.push({ QuestionId: $("#questionOption").val(), FundId: $("#fundOption").val(), AnswerValue: $("#answerOption").val() });
//                    var d = ko.toJSON(Model.FundAnswerSheet);
//                    var a = $.parseJSON(d);

//                    $.ajax({
//                        url: '/Fund/SaveFundAnswerSheet', type: "POST",
//                        data: JSON.stringify(a),
//                        contentType: 'application/json; charset=utf-8',
//                        success: function (data) { //Close if successful
//                            $(this).dialog('close'); //Close the dialog
//                        },
//                        error: function () { $(this).dialog('close'); } //Close the dialog}
//                    });
                    $(this).dialog('close');
                },
                Cancel: function () { //Close the dialog and reset controls when the cancel button is clicked
                    $(this).dialog('close'); //Close the dialog
                }
            }
        });
   }

   function SetChangeEvents(){
    /* Change Event Wired */

        /*  -----------------fundRuleMapping----------------------------------------------*/
        Fund_Rule_Map_Category.change(function () { GetMapQuestionGroupForCategory(); GetQuestionsForGroup(); }); //Add change event for the select control of the Question mapping dialog
        Fund_Rule_QuestionGroup_Option.change(function () { GetQuestionsForGroup(); }); //attach a change event for the question group

        //------------------- Current Rules Dropdowns -------------------------//
        Fund.change(function () { GetMappedQuestions(); }); //selected change event to get the mapping
        FundQuestionGroup.change(function () { GetMappedQuestions();  }); //selected change event to get the mapping
        
        //*-------Create Question----------*//
        Create_Question_Category.change(function () {GetQuestionGroupForCategory();});
   }

     

   /* -----------------Get the currently mapped Questions, this is the questions for the fund and group----------------------------------------------------------------------------------*/
    function GetMappedQuestions() {
        $.ajax({ url: '/Fund/GetQuestionMapping', type: "POST", contentType: "application/json; charset=utf-8", dataType: "json",
            data: "{ data: " + Fund.val() + ", groupId: " + FundQuestionGroup.val() + "}",
            success: function (data) {
                currentArray = data;
                
//                Current_Questions.find("tr").remove(); //Clean up the control
//                Current_Questions.find("td").remove(); //Clean up the control
//                $.each(data, function () {
//                    $.each(this, function (k, v) {
//                        Current_Questions.append("<tr><td>" + v.Description + "</td><td>" + v.Answer + "</td><td>" + v.AnswerType + "</td><td></td></tr>");
//                    });
//                });

            }, error: function () { alert("failed"); }
        } );

//        $("#list2").jqGrid( {
//            url: '/Fund/GetQuestionMapping',
//            data: "{ data: " + Fund.val() + ", groupId: " + FundQuestionGroup.val() + "}",
//            datatype: "json",
//            colNames: ['questionDescription', 'Answer', 'AnswerType'],
//            colModel: [
//   		{ name: 'questionDescription', index: 'questionDescription', width: 55 },
//   		{ name: 'Answer', index: 'Answer', width: 90 },
//   		{ name: 'AnswerType', index: 'AnswerType', width: 100}],
//            rowNum: 10,
//            rowList: [10, 20, 30],
//            pager: '#pager2',
//            sortname: 'questionDescription',
//            viewrecords: true,
//            sortorder: "desc",
//            caption: "JSON Example"
//        } );


    


        //$("button").button();
//        oTable = $('#grid').dataTable({
//            "bJQueryUI": true,
//            "bRetrieve": true,
//            "aaData": currentArray,
//            "sPaginationType": "full_numbers"
//        });
        
}

function addFormData() {
    var Model = { MapQuestions: ko.observableArray([]), add: function () { } };
    Model.MapQuestions.push({ FundId: $("#fund").val(), QuestionGroupId: $("#questionGroup").val() });
    var d = ko.toJSON(Model.MapQuestions);
    var a = $.parseJSON(d);
    var dt = JSON.stringify(a)
    //passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from
    //$('#sform').serializeArray();
   // alert(dt);
   
    return true;
}

    function GetMapQuestionGroupForCategory() {//Get the Question Groups for the Question Category
        var Model = { QuestionCategory: ko.observableArray([]), add: function () { } };
        Model.QuestionCategory.push({ QuestionCategoryId: $("#questionCategoryption").val() });
        var d = ko.toJSON(Model.QuestionCategory);
        var a = $.parseJSON(d);
        $.ajax({
            url: '/Fund/GetQuestionGroupForCategory', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#questionGroupOption").find("option").remove(); //Clean up the control
                $.each(data, function () {
                    $.each(this, function (k, v) {
                        $("#questionGroupOption").append("<option value=" + v.QuestionGroupId + ">" + v.Description + "</option>");
                    });
                });
                $("#questionGroupOption").selectmenu({}); //set the control to look like the rest
            }
        });
    }

    function saveCreateRule() {//Create the question function is called when the save button is clicked from the dialog
        var isGeneral = $(".checkbox").children("input").attr("checked");
        alert(isGeneral);
        
        var Model = { Question: ko.observableArray([]), add: function () { } };
        Model.Question.push({ QuestionGroupId: $("#QuestionGroupForm").val(), Class: "A", Question1: $("#questionDescription").val(), AnswerDataTypeId: $("#AnswerDataType").val(), AnswerFromList: true });
        var d = ko.toJSON(Model.Question);
        var a = $.parseJSON(d);
        $.ajax({
            url: '/Fund/SaveQuestion', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
            },
            error: function () { alert("Failed"); }
        });
    }

    function GetQuestionGroupForCategory() {//Get the Question Groups for the Question Category
        var Model = { QuestionCategory: ko.observableArray([]), add: function () { } };
        Model.QuestionCategory.push({ QuestionCategoryId: $("#questionCategory").val() });
        var d = ko.toJSON(Model.QuestionCategory);
        var a = $.parseJSON(d);
        $.ajax({
            url: '/Fund/GetQuestionGroupForCategory', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#QuestionGroupForm").find("option").remove(); //Clean up the control
                $.each(data, function () {
                    $.each(this, function (k, v) {
                        $("#QuestionGroupForm").append("<option value=" + v.QuestionGroupId + ">" + v.Description + "</option>");
                    });
                });
                $("#QuestionGroupForm").selectmenu({}); //set the control to look like the rest
            }
        });
    }

   

    function ShowMapDialog() {//Show the mapping dialog for mapping the question to a fund
        $("#fundRuleMapping").dialog('open'); $("#fundOption").selectmenu({}); $("#questionCategoryption").selectmenu({}); GetMapQuestionGroupForCategory(); $("#questionOption").selectmenu({});
        GetQuestionsForGroup();
    }

    function GetQuestionsForGroup() {//Get the question for the selected question group
        var Model = { QuestionGroup: ko.observableArray([]), add: function () { } };
        Model.QuestionGroup.push({ QuestionGroupId: $("#questionGroupOption").val() });
        var d = ko.toJSON(Model.QuestionGroup);
        var a = $.parseJSON(d);
        $.ajax({
            url: '/Fund/GetQuestionForQuestionGroup', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#questionOption").find("option").remove(); //Clean up the control
                $.each(data, function () {
                    $.each(this, function (k, v) {
                        $("#questionOption").append("<option value=" + v.QuestionId + ">" + v.Description + "</option>");
                    });
                });
                $("#questionOption").selectmenu({}); //set the control to look like the rest
            }
        });
    }

    function showCatelogMap() {
        $("#CreateMapForCategory").dialog('open'); $("#mapQuestioCategory").selectmenu({});
    }

    function GetQuestions() {//Get the questions for the fund and the question group

    }

    function DeleteMap(fundSheetId) {
        var Model = { FundAnswerSheet: ko.observableArray([]), add: function () { } };
        Model.FundAnswerSheet.push({ FundAnswerSheetId: fundSheetId });
        var d = ko.toJSON(Model.FundAnswerSheet);
        var a = $.parseJSON(d);

        $.ajax({
            url: '/Fund/DeleteFundAnswerSheet', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) { //Close if successful

            },
            error: function () { } //Close the dialog}
        });
        GetMappedQuestions(); //Page load to get the mapping
        //alert(fundSheetId);
    }

   

    

</script>
